package smedi.ismedi.web.bff.controller.pesonnel;

import com.smedi.ismedi.authority.starter.util.AuthorityUtil;
import com.smedi.ismedi.authoritymanagement.api.dto.response.RoleBasicResponse;
import com.smedi.ismedi.authoritymanagement.api.feign.RemoteAuthorityRoleManagementFeignApi;
import com.smedi.ismedi.constructionproject.api.dto.response.ProjectAssignmentResponse;
import com.smedi.ismedi.constructionproject.api.feign.RemoteProjectAssignmentFeignApi;
import com.smedi.ismedi.personnel.api.feign.RemotePersonValidateFeignApi;
import com.smedi.ismedi.web.starter.common.ResponseData;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiImplicitParam;
import io.swagger.annotations.ApiImplicitParams;
import io.swagger.annotations.ApiOperation;
import lombok.AllArgsConstructor;
import lombok.extern.slf4j.Slf4j;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;
import smedi.ismedi.web.bff.controller.BaseController;

import java.util.ArrayList;
import java.util.List;
import java.util.Optional;
import java.util.stream.Collectors;

/**
 * 人员校验接口
 *
 * @author 徐林俊
 */
@Slf4j
@RestController
@Api(tags = {"人员校验接口"})
@RequestMapping("/webbff/v1/personnelValidate")
@AllArgsConstructor
public class PersonValidateController extends BaseController {
    private final RemotePersonValidateFeignApi remotePersonValidateFeignApi;
    private final RemoteAuthorityRoleManagementFeignApi remoteAuthorityRoleManagementFeignApi;
    private final RemoteProjectAssignmentFeignApi remoteProjectAssignmentFeignApi;

    private final AuthorityUtil authorityUtil;

    /**
     * 校验人员是否有效
     *
     * @param personId
     * @return
     */
    @ApiOperation(httpMethod = "GET", value = "校验人员是否有效")
    @ApiImplicitParams({
            @ApiImplicitParam(paramType = "query", name = "personId", value = "人员id", dataType = "Long", dataTypeClass = Long.class),
    })
    @GetMapping(value = "/checkPersonAvailable")
    public ResponseData<Boolean> getCheckPersonAvailable(@RequestParam(value = "personId") Long personId) {
        return remotePersonValidateFeignApi.getCheckPersonAvailable(personId);
    }

    /**
     * 判断人员是否为企业人员
     *
     * @param personId
     * @return
     */
    @ApiOperation(httpMethod = "GET", value = "判断人员是否为企业人员")
    @ApiImplicitParams({
            @ApiImplicitParam(paramType = "query", name = "personId", value = "人员id", dataType = "Long", dataTypeClass = Long.class),
    })
    @GetMapping(value = "/checkPersonIsInternal")
    public ResponseData<Boolean> getCheckPersonIsInternal(@RequestParam(value = "personId") Long personId) {
        return remotePersonValidateFeignApi.getCheckPersonIsInternal(personId);
    }

    /**
     * 判断人员是否为项目人员
     *
     * @param personId
     * @return
     */
    @ApiOperation(httpMethod = "GET", value = "判断人员是否为项目人员")
    @ApiImplicitParams({
            @ApiImplicitParam(paramType = "query", name = "personId", value = "人员id", dataType = "Long", dataTypeClass = Long.class),
    })
    @GetMapping(value = "/checkPersonIsExternal")
    public ResponseData<Boolean> getCheckPersonIsExternal(@RequestParam(value = "personId") Long personId) {
        return remotePersonValidateFeignApi.getCheckPersonIsExternal(personId);
    }

    /**
     * 判断企业组织下是否有员工分配
     *
     * @param hrOrganizationId
     * @return
     */
    @ApiOperation(httpMethod = "GET", value = "判断企业组织下是否有员工分配")
    @ApiImplicitParams({
            @ApiImplicitParam(paramType = "query", name = "hrOrganizationId", value = "企业组织id", dataType = "Long", dataTypeClass = Long.class),
    })
    @GetMapping(value = "/checkHrOrganizationHasPerson")
    public ResponseData<Boolean> getCheckHrOrganizationHasPerson(@RequestParam(value = "hrOrganizationId") Long hrOrganizationId) {
        return remotePersonValidateFeignApi.getCheckHrOrganizationHasPerson(hrOrganizationId);
    }

    /**
     * 判断人员是否为项目经理且有项目存在
     *
     * @param personId 人员id
     * @return ResponseData<Boolean>
     */
    @ApiOperation(httpMethod = "GET", value = "判断人员是否为项目经理且有项目存在")
    @ApiImplicitParams({
            @ApiImplicitParam(paramType = "query", name = "personId", value = "人员id", dataType = "Long", dataTypeClass = Long.class),
    })
    @GetMapping(value = "/checkPersonIsManagerAndHaveProject")
    public ResponseData<Boolean> getCheckPersonIsManagerAndHaveProject(@Validated @RequestParam(value = "personId") Long personId) {
        boolean flagA = false;
        boolean flagB = false;

        // 判断是否是项目经理
        Optional<ResponseData<List<RoleBasicResponse>>> queryRolesByPersonId = Optional.of(remoteAuthorityRoleManagementFeignApi.getQueryRolesByPersonId(personId));
        List<RoleBasicResponse> roleBasicResponses = queryRolesByPersonId.map(ResponseData::getData).orElse(new ArrayList<>());

        if (roleBasicResponses.isEmpty()) {
            return new ResponseData<>().success(false);
        }

        List<Long> roleIds = roleBasicResponses.stream().map(RoleBasicResponse::getRoleId).collect(Collectors.toList());
        if (roleIds.contains(14L) || roleIds.contains(16L)) {
            flagA = true;
        }

        // 判断是否有项目存在
        ResponseData<List<ProjectAssignmentResponse>> queryProjectAssignmentsByPersonId = remoteProjectAssignmentFeignApi.getQueryProjectAssignmentsByPersonId(personId);
        if (!queryProjectAssignmentsByPersonId.getData().isEmpty()) {
            flagB = true;
        }

        return new ResponseData<>().success(flagA && flagB);
    }
}
